﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Xeml.Document
{
    public class DynamicTerm : Term
    {
        DynamicValueCollection _values;
        public DynamicValueCollection Values
        {
            get
            {
                return _values;
            }
        }

        public DynamicTerm(string termId)
            : base(termId)
        {
            _values = new DynamicValueCollection();
            _values.CollectionChanged += (sender, args) =>
            {
                OnPropertyChanged("Values");
            };
        }

        public DynamicTerm(DynamicTerm term)
            : base(term)
        {
            _values = new DynamicValueCollection();
            _values.CollectionChanged += (sender, args) =>
            {
                OnPropertyChanged("Values");
            };
        }

        /// <summary>
        /// Lists all contexts.
        /// </summary>
        /// <returns></returns>
        public List<string> ContextList()
        {
            List<string> ret = new List<string>();
            foreach (DynamicValueBase vb in _values)
            {
                if (!ret.Contains(vb.Context))
                    ret.Add(vb.Context);
            }
            return ret;
        }

        /// <summary>
        /// Returns all values of an given context.
        /// </summary>
        /// <param name="context">The context to filter.</param>
        /// <returns>A list of values.</returns>
        public List<DynamicValueBase> ValueBy(string context)
        {
            return _values.FindAll(delegate(DynamicValueBase vb) { return vb.Context == context; });
        }
    }
}
